package com.data.modules.smzh.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.data.common.api.vo.Result;
import com.data.common.system.query.QueryGenerator;
import com.data.common.system.vo.LoginUser;
import com.data.common.util.ConvertUtils;
import com.data.common.util.KamliClientUtil;
import com.data.modules.smzh.entity.SmComAnnouncement;
import com.data.modules.smzh.entity.SmPushMsg;
import com.data.modules.smzh.service.ISmPushMsgService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.data.common.system.base.controller.BootController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.data.common.aspect.annotation.AutoLog;

 /**
 * @Description: 时迈推送消息
 * @Author: data-boot
 * @Date:   2023-02-07
 * @Version: V1.0
 */
@Api(tags="时迈推送消息")
@RestController
@RequestMapping("/smzh/smPushMsg")
@Slf4j
public class SmPushMsgController extends BootController<SmPushMsg, ISmPushMsgService> {
	@Autowired
	private ISmPushMsgService smPushMsgService;

	/**
	 * 分页列表查询
	 *
	 * @param smPushMsg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="时迈推送消息-分页列表查询", notes="时迈推送消息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SmPushMsg smPushMsg,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SmPushMsg> queryWrapper = QueryGenerator.initQueryWrapper(smPushMsg, req.getParameterMap());
		Page<SmPushMsg> page = new Page<SmPushMsg>(pageNo, pageSize);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(ObjectUtil.notEqual(sysUser.getUsername(),"admin")){
			queryWrapper.eq("create_by",sysUser.getUsername());
		}
		IPage<SmPushMsg> pageList = smPushMsgService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param smPushMsg
	 * @return
	 */
	@AutoLog(value = "时迈推送消息-添加")
	@ApiOperation(value="时迈推送消息-添加", notes="时迈推送消息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SmPushMsg smPushMsg) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		smPushMsg.setAppId(sysUser.getAppId());
		smPushMsgService.save(smPushMsg);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param smPushMsg
	 * @return
	 */
	@AutoLog(value = "时迈推送消息-编辑")
	@ApiOperation(value="时迈推送消息-编辑", notes="时迈推送消息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SmPushMsg smPushMsg) {
		smPushMsgService.updateById(smPushMsg);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "时迈推送消息-通过id删除")
	@ApiOperation(value="时迈推送消息-通过id删除", notes="时迈推送消息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		smPushMsgService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "时迈推送消息-批量删除")
	@ApiOperation(value="时迈推送消息-批量删除", notes="时迈推送消息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.smPushMsgService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	 @ApiOperation(value="批量推送门口机基本信息", notes="批量推送门口机基本信息")
	 @RequestMapping(value = "/push", method = RequestMethod.PUT)
	 public Result<?> push(@RequestBody JSONObject jsonObject) {

		 String ids =  jsonObject.getString("id");
		 String model =  jsonObject.getString("model");
		 String projectNo = jsonObject.getString("projectNo");

		 smPushMsgService.pushMsg(ids,model,projectNo);
		 return Result.OK("推送成功!");
	 }

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "时迈推送消息-通过id查询")
	@ApiOperation(value="时迈推送消息-通过id查询", notes="时迈推送消息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SmPushMsg smPushMsg = smPushMsgService.getById(id);
		if(smPushMsg==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(smPushMsg);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param smPushMsg
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmPushMsg smPushMsg) {
        return super.exportXls(request, smPushMsg, SmPushMsg.class, "时迈推送消息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmPushMsg.class);
    }

}
